import React, {Component} from "react";
import PubSub from 'pubsub-js'
import axios from "axios";


export default class Search extends Component {

    search = () => {

        // 获取用户输入（连续解构赋值+重命名）
        const {keyWordElement: {value: keyWord}} = this
        // 发送请求前通知List更新状态
        PubSub.publish('zhouxiaoge', {isFirst: false, isLoading: true});
        // 发送网络请求
        axios.get(`https://api.github.com/search/users?q=${keyWord}`).then(
            response => {
                // 请求成功后通知Listp更新状态
                PubSub.publish('zhouxiaoge', {isLoading: false, users: response.data.items});
            },
            error => {
                // 请求失败后通知List更新状态
                PubSub.publish('zhouxiaoge', {isLoading: false, err: error.message});
            }
        )
    }

    render() {
        return (
            <section className="jumbotron">
                <h3 className="jumbotron-heading">搜索github用户</h3>
                <div>
                    <input ref={c => this.keyWordElement = c} type="text" placeholder="输入关键字点击进行搜索"/>&nbsp;
                    <button onClick={this.search}>Search</button>
                </div>
            </section>
        )
    }
};